package com.woniu.stafforder.netty;

import com.alibaba.fastjson2.JSON;
import com.woniu.stafforder.config.SpringContext;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class MyServerHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
    private static final Map<String, Channel> channelMap = new HashMap<>();
    private String userName;
    Jedis jedis = new Jedis("47.116.164.60",4070);
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        channelMap.put(ctx.channel().id().toString(), ctx.channel());
        log.info("频道新建成功, 频道编号为 {}", ctx.channel().id());
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        channelMap.remove(ctx.channel().id().toString());
        log.info("频道编号 {} 与服务端断开连接", ctx.channel().id());
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {
        jedis.auth("meng18952939913M");
        jedis.select(1);
        Map<String, Object> receivedData = JSON.parseObject(msg.text(), HashMap.class);
        String type = (String) receivedData.get("type");
        String userName = (String) receivedData.get("userName");
        String to = (String) receivedData.get("to");
        //用户发送的消息
        String newMessage = (String) receivedData.get("message");
        if (type.equals("message")&&to!=null&&newMessage!=null){
            ctx.channel().writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(receivedData)));
            log.info("用户发送了消息:{}",newMessage);
        }
        if (type.equals("KeFu")&&userName!=null){
            jedis.hset("KeFu",userName,ctx.channel().id().toString());
            jedis.close();
        }else if (type.equals("YongHu")&&userName!=null){
            jedis.hset("YongHu",userName,ctx.channel().id().toString());
            jedis.close();
        }
        //客服退出连接
        if (type.equals("KeFuCL")&&userName!=null){
            jedis.hdel("KeFu",userName);
            jedis.close();
        }else if (type.equals("YongHuCL")&&userName!=null){
            jedis.hdel("YongHu",userName);
            jedis.close();
        }
        if (type != null && to != null) {
            Channel toChannel = channelMap.get(to);
            if (toChannel != null) {
                receivedData.put("from", ctx.channel().id().toString());
                toChannel.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(receivedData)));
            }
        }
    }
}
